﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Devfw.OpenAuth.Platform;
using System.Configuration;
using WebSite.Core;

namespace WebSite.ThirdPartyLogin
{
    
    public partial class SinaCallback : Page
    {
        public class SinaReturn
        {
            public string access_token
            {
                get;
                set;
            }

            public string remind_in
            {
                get;
                set;
            }

            public string expires_in
            {
                get;
                set;
            }

            public string uid
            {
                get;
                set;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            string apiKey = ConfigurationManager.AppSettings["SinaApiKey"];
            string apiSecret = ConfigurationManager.AppSettings["SinaApiSecret"];
            string callback = ConfigurationManager.AppSettings["SinaCallbackUrl"]; 

            /*
            var oauth = new NetDimension.Weibo.OAuth(apiKey, apiSecret, callback);
            var result = oauth.GetAccessTokenByAuthorizationCode(Request["code"]);

            Response.Write(string.Format("UID={0},Token={1},RefreshToken={2},ExpiresIn={3}", result.UID, result.Token, result.RefreshToken, result.ExpiresIn));
            */
            
            SinaV2 sina = new SinaV2(apiKey, apiSecret);
            var result = sina.getAccessToken(Request.QueryString["code"], callback);
            // Response.Write(result);
            var rt = JsonHelper.DeserializeToObj<SinaReturn>( result );

            LoginHelper.SetCurrentUser( "sina$$$" + rt.uid );
        }
    }
}